const Koa = require('koa')
const Router = require('koa-router')
const { getHome, getList} = require('./getPipiData')

const app = new Koa()
const router = new Router()

const nunjucks = require('nunjucks')

router.get('/', async (ctx, next) => {
    next()
    try {
        let homeData = await getHome()
        // ctx.body = homeData
        ctx.body = nunjucks.render('views/index.html', { homeData })
    } catch (error) {
        console.log(error);
        ctx.body = {
            code: 1001
        }
    }
})
router.get('/:id/:page', async (ctx, next) => {
    try {
        let { id, page = 1 } = ctx.params   
        let data = await getList({id, page})
        // ctx.body = data
        ctx.body = nunjucks.render('views/list.html', { data })
    } catch (error) {
        console.log(error);
        ctx.body = {
            code: 1001
        }
    }
    next()
})

app
    .use(router.routes())
    .use(router.allowedMethods())
    .listen(3001)
console.log('http://localhost:3001')
